/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package east2west.dao;

import east2west.dto.CustomerDTO;
import east2west.dto.TourOrderDTO;
import east2west.interfaces.TourOrderInterface;
import east2west.util.DatabaseUtil;
import east2west.util.DateTimeUtil;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author thanhluan
 */
public class TourOrderDAO implements TourOrderInterface {

    Connection con = null;
    CallableStatement call = null;
    ResultSet rs = null;

    public ArrayList<TourOrderDTO> readByAll() {
        try {
            ArrayList<TourOrderDTO> list = new ArrayList<TourOrderDTO>();
            con = DatabaseUtil.getConnection();
            call = con.prepareCall("select * from V_TourOrder_Info where IsDeleted=0");
            rs = call.executeQuery();
            while (rs.next()) {
                TourOrderDTO dto = new TourOrderDTO();
                dto.setOrderId(rs.getInt(1));
                CustomerDAO cusDAO = new CustomerDAO();
                CustomerDTO cusDTO = cusDAO.findById(rs.getInt(2));
                dto.setCusDTO(cusDTO);
                Date date = rs.getDate(3);
                if (date == null) {
                    dto.setOrderDate("");
                } else {
                    dto.setOrderDate(DateTimeUtil.convert2String(date, "MM/dd/yyyy"));
                }
                // dto.setIsDelete(rs.getBoolean(4));
                list.add(dto);
            }
            return list;
        } catch (SQLException ex) {
            return null;
        }
    }

    public ArrayList<TourOrderDTO> findByOrderDate(String orderDate) {
        try {
            ArrayList<TourOrderDTO> list = new ArrayList<TourOrderDTO>();
            con = DatabaseUtil.getConnection();
            call = con.prepareCall("{call sp_Call_View_TourOrder_FindOrderDate(?)}");
            call.setDate(1, DateTimeUtil.convert2Date(orderDate, "dd-MM-yyyy"));
            rs = call.executeQuery();
            while (rs.next()) {
                TourOrderDTO dto = new TourOrderDTO();
                dto.setOrderId(rs.getInt(1));
                dto.setCustId(rs.getInt(2));
                Date date = rs.getDate(3);
                if (date == null) {
                    dto.setOrderDate("");
                } else {
                    dto.setOrderDate(DateTimeUtil.convert2String(date, "dd-MM-yyyy"));
                }
                dto.setIsDelete(rs.getBoolean(4));
                list.add(dto);
            }
            return list;
        } catch (SQLException ex) {
            return null;
        }
    }

    public TourOrderDTO findById(int id) {

        try {
            TourOrderDTO dto = null;
            con = DatabaseUtil.getConnection();
            call = con.prepareCall("select * from V_TourOrder_Info where IsDeleted=1 and OrderId = ?");
            call.setInt(1, id);
            rs = call.executeQuery();
            while (rs.next()) {
                dto = new TourOrderDTO();
                dto.setOrderId(rs.getInt(1));
                dto.setCustId(rs.getInt(2));
                Date date = rs.getDate(3);
                if (date == null) {
                    dto.setOrderDate("");
                } else {
                    dto.setOrderDate(DateTimeUtil.convert2String(date, "MM/dd/yyyy"));
                }
                dto.setIsDelete((rs.getBoolean(4)));

            }
            return dto;
        } catch (SQLException ex) {
            return null;
        }
    }

    public ArrayList<TourOrderDTO> findByOrderDto(TourOrderDTO dto) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int create(TourOrderDTO dto) {
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall("{call sp_Insert_TourOrders(?,?,?)}");
            call.setInt(1, dto.getCustId());
            call.setString(2, dto.getOrderDate());
            //call.setBoolean(3, dto.getIsDelete());
            call.registerOutParameter(3, java.sql.Types.INTEGER);
            call.executeUpdate();
            return call.getInt(3);


        } catch (SQLException ex) {

            return 0;
        } finally {
            try {
                if (con != null && !con.isClosed()) {
                    DatabaseUtil.closeConnection(con);
                }

            } catch (SQLException ex) {
            }
        }
    }

    public boolean update(TourOrderDTO dto) {
        try {
            con = DatabaseUtil.getConnection();
            con.setAutoCommit(false);
            call = con.prepareCall("{call sp_Update_TourOrder(?,?,?)}");

            call.setInt(1, dto.getOrderId());
            call.setInt(2, dto.getCustId());
            call.setDate(3, DateTimeUtil.convert2Date(dto.getOrderDate(), "dd-MM-yyyy"));
            call.setBoolean(4, dto.getIsDelete());
            call.executeUpdate();
            con.commit();
            return true;
        } catch (SQLException ex) {
            if (con != null) {
                try {
                    con.rollback();
                } catch (SQLException ex1) {
                }
            }
            return false;
        } finally {
            try {
                if (con != null && !con.isClosed()) {
                    DatabaseUtil.closeConnection(con);
                }

            } catch (SQLException ex) {
            }
        }
    }

    public boolean delete(TourOrderDTO dto) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean delete(int id) {
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall("DELETE FROM TourOrders WHERE OrderId =?");
            call.setInt(1, id);
            call.executeUpdate();
            return true;
        } catch (SQLException ex) {
            return false;
        } finally {
            try {
                if (con != null && !con.isClosed()) {
                    DatabaseUtil.closeConnection(con);
                }
            } catch (SQLException ex) {
            }
        }
    }

    public boolean acceptOrder(int id) {
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall("UPDATE TourOrders   SET IsDeleted = 1 WHERE OrderId =?");
            call.setInt(1, id);
            call.executeUpdate();
            return true;
        } catch (SQLException ex) {

            return false;
        } finally {
            try {
                if (con != null && !con.isClosed()) {
                    DatabaseUtil.closeConnection(con);
                }
            } catch (SQLException ex) {
            }
        }
    }
}
